package com.gitee.grower.util.germ

import com.germ.germplugin.api.dynamic.gui.GermGuiButton
import com.germ.germplugin.api.dynamic.gui.GermGuiScreen
import com.germ.germplugin.api.dynamic.gui.GermGuiScreen.ClickType
import com.gitee.grower.util.germ.germ.GermPartDSL
import ink.ptms.chemdah.util.conf
import taboolib.common5.Coerce
import taboolib.library.configuration.ConfigurationSection

class GermButtonDSL : GermPartDSL<GermGuiButton>() {

    var width = ""
    var height = ""
    var defaultPath = ""
    var hoverPath = ""
    var hoverColor : Long? = null
    var hoverSound = ""
    var clickSound = ""
    var fontSize = "27"
    var align = "center"
    var textScale = "1"
    var hoverAlign = "center"
    var hoverTexts = mutableListOf<String>()
    var texts = mutableListOf<String>()
    var tooltip = mutableListOf<String>()
    var textShadow = true
    var invalid = true

    var hoverOmitWidth : String? = null
    var omitWidth : String? = null

    private var onClick: ((GermGuiScreen) -> Unit)? = null

    override val type: String = "texture"

    fun onClick(func: (GermGuiScreen) -> Unit) {
        this.onClick = func
    }

    override fun infuse(config: ConfigurationSection) {
        name = config.name
        config.getString("locationX")?.let { locationX = it }
        config.getString("locationY")?.let { locationY = it }
        config.getString("locationZ")?.let { locationZ = it }
        config.getString("defaultPath")?.let { defaultPath = it }
        config.getString("hoverPath")?.let { hoverPath = it }
        config.getString("fontSize")?.let { fontSize = it }
        config["hoverColor"]?.let {
            hoverColor = Coerce.toLong(it)
        }
        config.getString("hoverSound")?.let { hoverSound = it }
        config.getString("clickSound")?.let { clickSound = it }
        config.getString("hoverAlign")?.let { hoverAlign = it }
        config.getString("textScale")?.let { textScale = it }
        config.getString("align")?.let { align = it }
        config.getString("width")?.let { width = it }
        config.getString("height")?.let { height = it }
        config.getString("width")?.let { width = it }

        hoverOmitWidth = config.getString("hoverOmitWidth")
        omitWidth = config.getString("omitWidth")
        texts += config.getStringList("texts")
        hoverTexts += config.getStringList("hoverTexts")
        tooltip += config.getStringList("tooltip")

        textShadow = config.getBoolean("textShadow")
        invalid = config.getBoolean("invalid")

        enable = config.getBoolean("enable")
    }

    override fun create(): GermGuiButton {
        return GermGuiButton(name).also {
            it.locationX = locationX
            it.locationY = locationY
            it.locationZ = locationZ
            it.enable = enable
            it.width = width
            it.height = height
            it.clickSound = clickSound
            it.defaultPath = defaultPath
            it.hoverPath = hoverPath
            hoverColor?.apply { it.hoverColor = this }
            it.hoverSound = hoverSound
            it.clickSound = clickSound
            it.align = align
            it.textScale = textScale
            it.hoverAlign = hoverAlign
            it.hoverTexts = hoverTexts
            it.texts = texts
            it.tooltip = tooltip
            it.hoverOmitWidth = hoverOmitWidth
            it.omitWidth = omitWidth
            it.isTextShadow = textShadow
            it.isInvalid = invalid
            onClick?.apply {
                it.registerCallbackHandler({ _, _ ->
                    onClick!!(it.germGuiScreen)
                }, ClickType.LEFT_CLICK)
            }
        }
    }
}